package com.powerunion.datacollection.persistence.db.jdbc.base;

import java.sql.*;

import org.apache.log4j.Logger;
import com.powerunion.util.DBUtils;
import com.powerunion.util.log.LogHelper;

/**
 * 数据库访问方法实现类
 * @author qyw
 *
 */
public class DBAccess {
	private static Logger logger = LogHelper.getInstance().getLogger(DBAccess.class);
  /*
    public static void queryForOneConnection(String selectSQL,
                                             ResultSetHandler handlerImp,
                                             Connection connection) throws
        DBAccessException {
      Connection conn = connection;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
        ps = conn.prepareStatement(selectSQL);
        rs = ps.executeQuery();
        if (logger.isDebugEnabled()) {
          logger.debug("执行查询语句：" + selectSQL);
        }
        while (rs.next()) {
          handlerImp.handle(rs); //处理结果集
        }
      }
      catch (SQLException sqle) {
        logger.error("执行查询语句[" + selectSQL + "]错误", sqle);
        throw new DBAccessException("执行查询语句错误", sqle);
      }
      finally {
        try {
          ConnectionFactory.closeAll(null, ps, rs);
        }
        catch (ConnectionException ce) {
          logger.error("关闭数据库连接出错！", ce);
        }
      }
    }
   */
  /*
    public static void query(String selectSQL, ResultSetHandler handlerImp,
                             Connection connection) throws
        DBAccessException {
      queryImp2(selectSQL, handlerImp, connection);
    }
   */
  /*
   private static void queryImp2(String selectSQL, ResultSetHandler handlerImp,
   Connection connection) throws DBAccessException {
      Connection conn = connection;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
        ps = conn.prepareStatement(selectSQL);
        rs = ps.executeQuery();
        if (logger.isDebugEnabled()) {
          logger.debug("执行查询语句：" + selectSQL);
        }
        while (rs.next()) {
          handlerImp.handle(rs); //处理结果集
        }
      }
      catch (SQLException sqle) {
        logger.error("执行查询语句[" + selectSQL + "]错误", sqle);
        throw new DBAccessException("执行查询语句错误", sqle);
      }
      finally {
        try {
          ConnectionFactory.closeAll(conn, ps, rs);
        }
        catch (ConnectionException ce) {
          logger.error("关闭数据库连接出错！", ce);
        }
      }
    }
   */
  public static void queryForOneConnection(IAccessManner selectAccessManner,
                                           ResultSetHandler handlerImp,
                                           Connection connection) throws
      DBAccessException {
    Connection con = connection;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      ps = selectAccessManner.accessByPreStatement(con);
      rs = ps.executeQuery();
      while (rs.next()) {
        handlerImp.handle(rs); //处理结果集
      }
    }
    catch (SQLException sqle) {
      logger.error("执行[" + selectAccessManner + "]错误", sqle);
      throw new DBAccessException("执行查询语句错误", sqle);
    }
    finally {
    	DBUtils.close(rs, ps);
    }
  }

  public static void query(IAccessManner selectAccessManner,
                           ResultSetHandler handlerImp,
                           Connection connection) throws
      DBAccessException {
    Connection con = connection;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      ps = selectAccessManner.accessByPreStatement(con);
//      if (logger.isDebugEnabled()) {
//        logger.debug("执行查询方法：[" + selectAccessManner + "]");
//      }
      rs = ps.executeQuery();
      while (rs.next()) {
        handlerImp.handle(rs); //处理结果集
      }
    }
    catch (SQLException sqle) {
      logger.error("执行查询方法[" + selectAccessManner + "]错误", sqle);
      throw new DBAccessException("执行查询语句错误", sqle);
    }
    finally {
    	DBUtils.close(rs, ps, con);
    }
  }

  /*
    public static int updateForOneConnection(String updateSQL,
                                             Connection connection) throws
        DBAccessException {
      int reRowNum = 0;
      Connection conn = connection;
      PreparedStatement ps = null;
      try {
        ps = conn.prepareStatement(updateSQL);
        if (logger.isDebugEnabled()) {
          logger.debug("执行：[" + updateSQL + "]");
        }
        reRowNum = ps.executeUpdate();
        ps.close();
        return reRowNum;
      }
      catch (SQLException sqle) {
        logger.error("执行语句[" + updateSQL + "]错误", sqle);
        throw new DBAccessException("执行查询语句错误", sqle);
      }
      finally {
        try {
          ConnectionFactory.closeAll(null, ps, null);
        }
        catch (ConnectionException ce) {
          logger.error("关闭数据库连接出错！", ce);
        }
      }
    }
   */
  /*
    public static int update(String updateSQL, Connection connection) throws
        DBAccessException {
      return updateImp2(updateSQL, connection);
    }

   private static int updateImp2(String updateSQL, Connection connection) throws
        DBAccessException {
      int reRowNum = 0;
      Connection conn = connection;
      PreparedStatement ps = null;
      try {
        ps = conn.prepareStatement(updateSQL);
        if (logger.isDebugEnabled()) {
          logger.debug("执行：[" + updateSQL + "]");
        }
        reRowNum = ps.executeUpdate();
        ps.close();
        return reRowNum;
      }
      catch (SQLException sqle) {
        logger.error("执行语句[" + updateSQL + "]错误", sqle);
        throw new DBAccessException("执行查询语句错误", sqle);
      }
      finally {
        try {
          ConnectionFactory.closeAll(conn, ps, null);
        }
        catch (ConnectionException ce) {
          logger.error("关闭数据库连接出错！", ce);
        }
      }
    }
   */
  public static int[] updateBatch(IAccessManner updateAccessManner,
                                  Connection connection) throws
      DBAccessException {
    return updateBatchImp(updateAccessManner, connection);
  }

  private static int[] updateBatchImp(IAccessManner updateAccessManner,
                                      Connection connection) throws
      DBAccessException {
    Connection con = connection;
    PreparedStatement ps = null;
    try {
      ps = updateAccessManner.accessByPreStatement(con);
//      if (logger.isDebugEnabled()) {
//        logger.debug("执行批量方法：[" + updateAccessManner + "]");
//      }
      int r[] = ps.executeBatch();
      ps.clearBatch();
      return r;
    }
    catch (SQLException sqle) {
      logger.error("执行批量方法[" + updateAccessManner + "]错误", sqle);
      throw new DBAccessException("执行查询语句错误", sqle);
    }
    finally {
    	DBUtils.close(ps, con);
    }
  }

  public static int updateForOneConnection(IAccessManner updateAccessManner,
                                           Connection connection) throws
      DBAccessException {
    int reRowNum = 0;
    Connection con = connection;
    PreparedStatement ps = null;
    try {
      ps = updateAccessManner.accessByPreStatement(con);
//      if (logger.isDebugEnabled()) {
//        logger.debug("执行方法：[" + updateAccessManner + "]");
//      }
      reRowNum = ps.executeUpdate();
      return reRowNum;
    }
    catch (SQLException sqle) {
      logger.error("执行方法[" + updateAccessManner + "]错误", sqle);
      throw new DBAccessException("执行查询语句错误", sqle);
    }
    finally {
    	DBUtils.close(ps);
    }
  }

  public static int update(IAccessManner updateAccessManner,
                           Connection connection) throws
      DBAccessException {
    return updateImp(updateAccessManner, connection);
  }

  private static int updateImp(IAccessManner updateAccessManner,
                               Connection connection) throws DBAccessException {
    int reRowNum = 0;
    Connection con = connection;
    PreparedStatement ps = null;
    try {
      ps = updateAccessManner.accessByPreStatement(con);
//      if (logger.isDebugEnabled()) {
//        logger.debug("执行方法：[" + updateAccessManner + "]");
//      }
      reRowNum = ps.executeUpdate();
      return reRowNum;
    }
    catch (SQLException sqle) {
      logger.error("执行方法[" + updateAccessManner + "]错误", sqle);
      throw new DBAccessException("执行查询语句错误", sqle);
    }
    finally {
    	DBUtils.close(ps,con);
    }
  }

}

